<template>
  <div style="width: 1200px;margin: 0 auto;height: 100vh;">
   <span style="display: flex;justify-content: space-between;padding: 10px 0;">
    <router-link to="/main/renshi/kaoqing/kqguanli/huizong" style="">返回</router-link>
    <router-link to="/main/renshi/kaoqing/kqguanli/huizong"
      style="margin-bottom: 20px; display: inline-block;">新增记录</router-link>
   </span>
    <h2>考勤汇总记录列表</h2>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="employeeId" label="工号" width="120"></el-table-column>
      <el-table-column prop="name" label="姓名" width="120"></el-table-column>
      <el-table-column prop="groupName" label="班组名称" width="120"></el-table-column>
      <el-table-column prop="lateCount" label="迟到次数" width="100"></el-table-column>
      <el-table-column prop="earlyLeaveCount" label="早退次数" width="100"></el-table-column>
      <el-table-column prop="absenteeismCount" label="旷工次数" width="100"></el-table-column>
      <el-table-column prop="totalWorkHours" label="总工时(H)" width="120"></el-table-column>
      <el-table-column prop="missedCardCount" label="漏卡次数" width="100"></el-table-column>
      <el-table-column prop="nightShiftAllowanceCount" label="夜班津贴补助次数" width="150"></el-table-column>
      <el-table-column label="操作" width="250" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 详情对话框 -->
    <el-dialog title="考勤汇总详情" :visible.sync="dialogVisible" width="70%">
      <div v-if="detailData">
        <el-descriptions :column="3" border>
          <el-descriptions-item label="工号">{{ detailData.employeeId }}</el-descriptions-item>
          <el-descriptions-item label="姓名">{{ detailData.name }}</el-descriptions-item>
          <el-descriptions-item label="班组名称">{{ detailData.groupName }}</el-descriptions-item>
          <el-descriptions-item label="迟到时间">{{ detailData.lateTime }}</el-descriptions-item>
          <el-descriptions-item label="迟到次数">{{ detailData.lateCount }}</el-descriptions-item>
          <el-descriptions-item label="早退时间">{{ detailData.earlyLeaveTime }}</el-descriptions-item>
          <el-descriptions-item label="早退次数">{{ detailData.earlyLeaveCount }}</el-descriptions-item>
          <el-descriptions-item label="旷工时间">{{ detailData.absenteeismTime }}</el-descriptions-item>
          <el-descriptions-item label="旷工次数">{{ detailData.absenteeismCount }}</el-descriptions-item>
          <el-descriptions-item label="年假(H)">{{ detailData.annualLeave }}</el-descriptions-item>
          <el-descriptions-item label="婚假(H)">{{ detailData.marriageLeave }}</el-descriptions-item>
          <el-descriptions-item label="产假(H)">{{ detailData.maternityLeave }}</el-descriptions-item>
          <el-descriptions-item label="丧假(H)">{{ detailData.funeralLeave }}</el-descriptions-item>
          <el-descriptions-item label="病假(H)">{{ detailData.sickLeave }}</el-descriptions-item>
          <el-descriptions-item label="事假(H)">{{ detailData.personalLeave }}</el-descriptions-item>
          <el-descriptions-item label="工伤(H)">{{ detailData.workInjuryLeave }}</el-descriptions-item>
          <el-descriptions-item label="停工待料(H)">{{ detailData.shutdownMaterialTime }}</el-descriptions-item>
          <el-descriptions-item label="公出(H)">{{ detailData.businessTripTime }}</el-descriptions-item>
          <el-descriptions-item label="节假日(H)">{{ detailData.holidayTime }}</el-descriptions-item>
          <el-descriptions-item label="正班(H)">{{ detailData.regularShiftTime }}</el-descriptions-item>
          <el-descriptions-item label="平时加班(H)">{{ detailData.weekdayOvertimeTime }}</el-descriptions-item>
          <el-descriptions-item label="周末加班(H)">{{ detailData.weekendOvertimeTime }}</el-descriptions-item>
          <el-descriptions-item label="节假日加班(H)">{{ detailData.holidayOvertimeTime }}</el-descriptions-item>
          <el-descriptions-item label="总工时(H)">{{ detailData.totalWorkHours }}</el-descriptions-item>
          <el-descriptions-item label="漏卡次数">{{ detailData.missedCardCount }}</el-descriptions-item>
          <el-descriptions-item label="夜班津贴补助次数">{{ detailData.nightShiftAllowanceCount }}</el-descriptions-item>
          <el-descriptions-item label="员工确认">{{ detailData.employeeConfirmation }}</el-descriptions-item>
          <el-descriptions-item label="统计开始日期">{{ formatDateTime(detailData.startDate) }}</el-descriptions-item>
          <el-descriptions-item label="统计结束日期">{{ formatDateTime(detailData.endDate) }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAttendanceSummaryList, getAttendanceSummaryDetail, deleteAttendanceSummary } from '@/api/attendanceSummary';

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      detailData: null
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        const response = await getAttendanceSummaryList({
          current: this.currentPage,
          size: this.pageSize
        });
        this.tableData = response.data.records;
        this.total = response.data.total;
      } catch (error) {
        this.$message.error('获取数据失败：' + error.message);
      }
    },
    async handleDetail(row) {
      try {
        const response = await getAttendanceSummaryDetail(row.id);
        this.detailData = response.data;
        this.dialogVisible = true;
      } catch (error) {
        this.$message.error('获取详情失败：' + error.message);
      }
    },
    handleEdit(row) {
      this.$router.push(`/main/renshi/kaoqing/kqguanli/huizong?id=${row.id}`);
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该考勤记录？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        await deleteAttendanceSummary(row.id);
        this.$message.success('删除成功');
        this.fetchData();
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('删除失败：' + error.message);
        }
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    formatDateTime(datetime) {
      if (!datetime) return '';
      return datetime.split('T')[0] + ' ' + datetime.split('T')[1].substring(0, 8);
    }
  }
};
</script>

<style scoped>
.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: right;
}
</style>